package com.gis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.entity.OceanBouyData;
import com.gis.entity.RoeData;
import com.gis.result.ResponseResult;
import com.gis.service.RoeDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequiredArgsConstructor
@RequestMapping("/roe_data")
@Tag(name = "仔稚鱼", description = "仔稚鱼数据接口")
@CrossOrigin(origins = "*")
public class RoeDataController {
    @Autowired
    private RoeDataService roeDataService;

    @Operation(summary = "批量添加仔稚鱼数据")
    @Parameters({
        @Parameter(name = "roeDataList", description = "仔稚鱼数据列表", required = true)
    })
    @PostMapping("/addBatch")
    public ResponseResult<String> AddRoeDataServeice(@RequestBody List<RoeData> roeDataList) {
        roeDataService.saveBatch(roeDataList);
        // roeDataService.save(roeData);
        return ResponseResult.success("insert successfully");
    }

    @Operation(summary = "获取所有仔稚鱼数据")
    @GetMapping("/all")
    public ResponseResult<List<RoeData>> GetAllRoeData() {
        List<RoeData> roeDatas = roeDataService.list();
        return ResponseResult.success(roeDatas);
    }

    @Operation(summary = "根据年份和季度获取仔稚鱼数据")
    @GetMapping("/getByTime")
    public ResponseResult<List<RoeData>> GetByTime(@RequestParam Integer year, @RequestParam String season) {
        QueryWrapper<RoeData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year).eq("season", season);
        List<RoeData> roeDatas = roeDataService.list(queryWrapper);
        return ResponseResult.success(roeDatas);
    }

    @Operation(summary = "更新仔稚鱼数据")
    @Parameters({
        @Parameter(name = "roeData", description = "仔稚鱼数据", required = true)
    })
    @PostMapping("/update")
    public ResponseResult<RoeData> UpdateRoeData(@RequestBody RoeData roeData) {
        roeDataService.saveOrUpdate(roeData);
        return ResponseResult.success(roeData);
    }

    @Operation(summary = "根据ID删除仔稚鱼数据")
    @Parameters({
        @Parameter(name = "id", description = "ID", required = true)
    })
    @PostMapping("/deleteById")
    public ResponseResult<String> DeleteRoeDataById(@RequestBody Object json) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(json));
        Integer id = jsonObject.getInteger("id");
        roeDataService.removeById(id);
        return ResponseResult.success("delete successfully");
    }
    

    @Operation(summary = "分页获取仔稚鱼数据")
    @Parameters({
        @Parameter(name = "pageNo", description = "页码", required = true),
        @Parameter(name = "pagesize", description = "每页数量", required = true)
    })
    @GetMapping("/listPage")
    public ResponseResult<IPage<RoeData>> ListPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        IPage<RoeData> page = new Page<>(pageNo, pageSize, true);
        return ResponseResult.success(roeDataService.page(page));
    }
}